'use strict'

let nunjucks = require('nunjucks');

let createEnv = function(path, opts){
    path = path || 'views';
    opts = opts || {};

    let envOptions = {
        watch: opts.watch === undefined ? false : opts.watch,
        autoescape: opts.autoescape === undefined ? true : opts.autoescape,
        throwOnUndefind: opts.throwOnUndefind === undefined ? true : opts.throwOnUndefind,
        noCache: opts.noCache === undefined ? false : opts.noCache
    }
    let env = nunjucks.configure(path,envOptions);

    return env;
}

module.exports = async(ctx,next)=>{
    ctx.render = function(view,modle){
        let env = createEnv("views",{autoescape:false});
        ctx.body = env.render(view,modle);       
    }
    await next();
}